package top.codestudy.viewtest.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.OverScroller;

import androidx.annotation.Nullable;

import top.codestudy.viewtest.common.Util;

public class ArcView extends View {
    private Context context;

    public ArcView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public ArcView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public ArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    public ArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
        init();
    }

    private void init() {

        paint.setColor(lightBlue);
        paint.setStrokeWidth(5f);
        paint.setStyle(Paint.Style.STROKE);

        textpaint.setColor(Color.GREEN);
        textpaint.setTextSize(Util.sp2px(context,18));

        paint2.setColor(Color.parseColor("#9B30FF"));
        paint2.setStyle(Paint.Style.STROKE);
    }

    private boolean isOk = false;

    private int mCurrentProgress =0;
    private int lightBlue = Color.parseColor("#8DEEEE");
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
    Paint textpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    int start = 0;
    int end = 0;
    int progress = 0;
    private float endtextWidth = 0;
    float padding = 40;
    float maxTextWidth = 30;
    float minTextWidth = 5;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        RectF rectF = new RectF();
        rectF.left = padding;
        rectF.top = padding;
        rectF.right = getMeasuredWidth()-padding;
        rectF.bottom = getMeasuredHeight()-padding;
        end = end+10;
        //绘制文本



        if (end < 360){
            canvas.drawArc(rectF,start,end,false,paint);
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    postInvalidate();
                }
            },100);

        }else {
            end = 360;
            long longprogress = Math.round(end  / 360.0 * 100) ;
            String textprogress = longprogress + "%";
            float measureText = textpaint.measureText(textprogress);
            float textX = getMeasuredWidth()/2.0f - measureText/2.0f;
            float textY = (getMeasuredHeight() / 2.0f) - ((textpaint.descent() + textpaint.ascent()) / 2.0f);
            canvas.drawText(textprogress,textX,textY,textpaint);

            float strokeWidth = paint.getStrokeWidth();
            if (strokeWidth > maxTextWidth){
                float strokeWidth2 = paint2.getStrokeWidth();
                if (strokeWidth2 > minTextWidth){
                    float textWidth = strokeWidth2 - 2.0f;
                    Log.i("textWidth1", "onDraw: getStrokeWidth ==> "+strokeWidth2+" ; textWidth ==> " + textWidth);
                    paint2.setStrokeWidth(textWidth);
                    canvas.drawArc(rectF,start,end,false,paint2);
                    postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            postInvalidate();
                        }
                    },100);
                }else {
                    float textWidth = strokeWidth2;
                    Log.i("textWidth1", "onDraw: getStrokeWidth ==> "+strokeWidth2+" ; textWidth ==> " + textWidth);
                    paint2.setStrokeWidth(textWidth);
                    canvas.drawArc(rectF,start,end,false,paint2);
                    isOk = true;
                }
            }else {
                float textWidth = strokeWidth + 2.0f;
                Log.i("textWidth", "onDraw: getStrokeWidth ==> "+strokeWidth+" ; textWidth ==> " + textWidth);
                paint.setStrokeWidth(textWidth);
                endtextWidth = textWidth;
                paint2.setStrokeWidth(endtextWidth);
                canvas.drawArc(rectF,start,end,false,paint);
                postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        postInvalidate();
                    }
                },100);
            }
            if (progressListener != null && isOk){
                progressListener.onUpdateProgress(100);
            }
            return;
        }
        long longprogress = Math.round(end  / 360.0 * 100) ;
        String textprogress = longprogress + "%";
        float measureText = textpaint.measureText(textprogress);
        float textX = getMeasuredWidth()/2.0f - measureText/2.0f;
        float textY = (getMeasuredHeight() / 2.0f) - ((textpaint.descent() + textpaint.ascent()) / 2.0f);
        canvas.drawText(textprogress,textX,textY,textpaint);
        if (progressListener != null){
            progressListener.onUpdateProgress(longprogress);
        }

    }

    public void reset(){
        end = 0;
        isOk = false;
        init();
        invalidate();
    }

    private ProgressListener progressListener;

    public ProgressListener getProgressListener() {
        return progressListener;
    }

    public void setProgressListener(ProgressListener progressListener) {
        this.progressListener = progressListener;
    }
    private OverScroller overScroller;
    public interface ProgressListener {
        void onUpdateProgress(long progress);
    }
}
